<template>
    <div>
        <h1>我的评论</h1>
        <div class="MyComment-table">
            <el-table :data="myCommentList" border height="250">
                <el-table-column prop="createTime" label="创建时间" width="170">
                </el-table-column>
                <el-table-column prop="courseName" label="课程名" width="200">
                </el-table-column>
                <el-table-column prop="content" label="评论内容" width="300">
                </el-table-column>
                <el-table-column prop="likeCount" label="点赞数" width="60">
                </el-table-column>
                <el-table-column prop="dislikeCount" label="被踩数" width="60">
                </el-table-column>
                <el-table-column label="编辑">
                    <template slot-scope="scope">
                        <el-button @click="deleteComment(scope.row)" type="danger">删除</el-button>
                    </template>
                </el-table-column>
                
            </el-table>
        </div>
    </div>
</template>

<script>
export default {
    created(){
        this.init()
    },
    data(){
        return{
            userId:'',
            myCommentList:[
                {
                    courseName:'测试',
                }
            ]
        }
    },
    methods:{
        init(){
            this.$axios.get("http://localhost:8000/getUserInfo", {
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
            }).then(res => {
                console.log(res.data)
                this.userId = res.data.id;
                this.getMyCommentList(this.userId)
            }).catch(err => {
                console.log(err)
                this.$message.error(err.response.data.message)
            })
        },
        getUserInfo() {
            this.$axios.get("http://localhost:8000/getUserInfo", {
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
            }).then(res => {
                console.log(res.data)
                this.userId = res.data.id;
            }).catch(err => {
                console.log(err)
                this.$message.error(err.response.data.message)
            })
        },
        getMyCommentList(myId){
            this.$axios.get("http://localhost:8000/comment/findByUserId/" + myId, {
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
                })
                .then(res => {
                    console.log(res.data)
                    this.myCommentList = res.data
                })
                .catch(err => {
                    console.log(err)
                    this.$message.error(err.response.data.message)
                })
                
        },
        getMyComments(){
            this.$axios.get("http://localhost:8000/comment/findByUserId/" + this.userId, {
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
                })
                .then(res => {
                    console.log(res.data)
                    this.myCommentList = res.data
                })
                .catch(err => {
                    console.log(err)
                    this.$message.error(err.response.data.message)
                })
        },
        deleteComment(row){
            this.$axios.delete("http://localhost:8000/comment/deleteComment/" + row.id, {
                headers: {
                    Authorization: sessionStorage.getItem('access-admin')
                }
                })
                .then(res => {
                    this.$message.success('删除成功')
                    this.getMyCommentList(this.userId)
                })
                .catch(err => {
                    console.log(err)
                    this.$message.error(err.response.data.message)
                })
        },
        test(){
            console.log(this.myCommentList)
        }
    }
    

}
</script>

<style lang="scss" scoped>
h1{
    text-align: left;
}

</style>